package com.chenyue.cm.news.controller;


import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.news.domain.JdNewsComment;
import com.chenyue.cm.news.domain.JdNewsCommentExample;
import com.chenyue.cm.news.service.JdNewsCommentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenyue-cm
 * @date 2021-07-20
 */
@RestController
@RequestMapping("/jdNewsComment")
public class JdNewsCommentController {

    private final JdNewsCommentService jdNewsCommentService;

    public JdNewsCommentController(JdNewsCommentService jdNewsCommentService) {
        this.jdNewsCommentService = jdNewsCommentService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdNewsComment> result = jdNewsCommentService.getPageBean(pageInfo, JdNewsComment.class);
        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     *
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdNewsComment vo) {
        boolean success = jdNewsCommentService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo 修改实体
     * @param request id
     *
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(JdNewsComment vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = jdNewsCommentService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     *
     * @param id 主键
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdNewsCommentExample aExample = new JdNewsCommentExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdNewsCommentService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdNewsCommentService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
	}

}
